﻿Imports System.ComponentModel
Imports System.Data.OleDb
Imports Microsoft.Win32

Friend Class OleDBConnectionProperties
    Inherits AdoDotNetConnectionProperties
    ' Methods
    Public Sub New()
        MyBase.New("System.Data.OleDb")
    End Sub

    Private Function CanResetProvider(ByVal component As Object) As Boolean
        Return False
    End Function

    Protected Overridable Overloads Function GetProperties(ByVal attributes As Attribute()) As PropertyDescriptorCollection
        Dim properties As PropertyDescriptorCollection = MyBase.GetProperties(attributes)
        If Me._disableProviderSelection Then
            Dim descriptor As PropertyDescriptor = properties.Find("Provider", True)
            If (Not descriptor Is Nothing) Then
                Dim index As Integer = properties.IndexOf(descriptor)
                Dim array As PropertyDescriptor() = New PropertyDescriptor(properties.Count - 1) {}
                properties.CopyTo(array, 0)
                array(index) = New DynamicPropertyDescriptor(descriptor, New Attribute() {ReadOnlyAttribute.Yes})
                TryCast(array(index), DynamicPropertyDescriptor).CanResetValueHandler = New CanResetValueHandler(AddressOf Me.CanResetProvider)
                properties = New PropertyDescriptorCollection(array, True)
            End If
        End If
        Return properties
    End Function

    Public Shared Function GetRegisteredProviders() As List(Of String)
        Dim enumerator As OleDbDataReader = OleDbEnumerator.GetEnumerator(Type.GetTypeFromCLSID(NativeMethods.CLSID_OLEDB_ENUMERATOR))
        Dim dictionary As New Dictionary(Of String, String)
        Using enumerator
            Do While enumerator.Read
                Select Case CInt(enumerator.Item("SOURCES_TYPE"))
                    Case 1, 3
                        dictionary.Item(TryCast(enumerator.Item("SOURCES_CLSID"), String)) = Nothing
                        Exit Select
                End Select
            Loop
        End Using
        Dim list As New List(Of String)(dictionary.Count)
        Dim key As RegistryKey = Registry.ClassesRoot.OpenSubKey("CLSID")
        Using key
            Dim pair As KeyValuePair(Of String, String)
            For Each pair In dictionary
                Dim key2 As RegistryKey = key.OpenSubKey((pair.Key & "\ProgID"))
                If (Not key2 Is Nothing) Then
                    Using key2
                        list.Add(TryCast(key2.GetValue(Nothing), String))
                        Continue For
                    End Using
                End If
            Next
        End Using
        list.Sort()
        Do While list.Contains("MSDASQL.1")
            list.Remove("MSDASQL.1")
        Loop
        Return list
    End Function


    ' Properties
    Public Property DisableProviderSelection As Boolean
        Get
            Return Me._disableProviderSelection
        End Get
        Set(ByVal value As Boolean)
            Me._disableProviderSelection = value
        End Set
    End Property

    Public Overrides ReadOnly Property IsComplete As Boolean
        Get
            Return (TypeOf MyBase.ConnectionStringBuilder.Item("Provider") Is String AndAlso (TryCast(MyBase.ConnectionStringBuilder.Item("Provider"), String).Length <> 0))
        End Get
    End Property


    ' Fields
    Private _disableProviderSelection As Boolean
End Class


